package com.xdwanj.service.impl;

import com.xdwanj.dao.WordDao;
import com.xdwanj.pojo.Word;
import com.xdwanj.service.CmdService;
import com.xdwanj.util.CmdConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Scanner;

@Service
public class CmdServiceImpl implements CmdService {

    @Autowired()
    private WordDao wordDao;

    public Scanner input = new Scanner(System.in);

    public void all() {
        List<Word> wordList = wordDao.findAll();
        wordList.forEach(System.out::println);
    }

    public void add() {
        System.out.println("按顺序输入：英文，中文，词性");
        wordDao.save(
                new Word().setEnglish(input.next())
                        .setChinese(input.next())
                        .setPhoneticSymbol(input.next())
        );
        System.out.println();
    }

    public void study() {
        List<Word> wordList = null;

        System.out.print("输入单词: ");
        String cmdTemp = input.next();
        wordList = wordDao.findWordByEnglish(cmdTemp);
        if (wordList != null && !wordList.isEmpty()) {
            Collections.sort(wordList);
            System.out.println(wordList.get(0));
        } else if (CmdConst.CLS.equals(cmdTemp)) {
            System.out.flush();
        } else {
            System.out.println("单词错误，或者未收录\n");
        }
    }
}